package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Chat;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.ChatMapper;
import cn.wolfcode.trip.base.service.IChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ChatServiceImpl implements IChatService {

    @Autowired
    private ChatMapper chatMapper;

    @Override
    public List<User> queryForListByUserId(User user) {
        List<Chat> chatList = chatMapper.selectForList(user);
        List<User> list = new ArrayList<>();
        // 判断是这个用户是sender还是receiver,添加对方进列表
        for (Chat chat : chatList) {
            if (chat.getSender().getId().equals(user.getId())) {
                list.add(chat.getReceiver());
            } else if (chat.getReceiver().getId().equals(user.getId())) {
                list.add(chat.getSender());
            }
        }
        return list;
    }
}
